/*
 * Copyright 2009 Yannick Stucki (yannickstucki.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yannickstucki.android.musicqueue.old;

import android.content.res.Resources;
import android.view.Menu;
import android.view.MenuItem;

import com.yannickstucki.android.musicqueue.R;
import com.yannickstucki.android.musicqueue.util.Logger;

/**
 * Creates the options menu and handles triggered calls.
 * 
 * @author Yannick Stucki (yannickstucki@gmail.com)
 * 
 */
public class OptionsMenuManager {

  /**
   * Clears the queue i.e. the items that would come in as the next songs.
   */
  public static final int CLEAR_QUEUE_ID = 0;

  /**
   * Enqueues all the songs selected by the typing filter. (If no filter is
   * applied, all the songs are selected, which is of course pointless.) This is
   * currently the only menu option in landscape and is not used in portrait
   * mode.
   */
  public static final int ENQUEUE_FULL_LIST_ID = 1;

  /**
   * Opens the settings dialog.
   */
  public static final int SETTINGS_ID = 2;

  /**
   * Opens the help dialog.
   */
  public static final int HELP_ID = 3;

  /**
   * Does the actual work if an option item has been clicked.
   */
  private OptionsMenuHandler optionsHandler;

  /**
   * The class needs a reference to a {@link OptionsMenuHandler} to be
   * functional so it can invoke calls on the handler if a options menu item got
   * clicked.
   */
  public OptionsMenuManager(OptionsMenuHandler optionsHandler) {
    this.optionsHandler = optionsHandler;
  }

  /**
   * Creates the options for portrait menu. Always called before the options
   * menu is shown.
   */
  public MenuItem createOptionsMenuPortrait(Menu menu, Resources resources) {
    MenuItem clearQueue = menu.add(0, CLEAR_QUEUE_ID, 0, resources.getString(R.string.clear_queue))
        .setIcon(R.drawable.ic_menu_close_clear_cancel);
    menu.add(0, HELP_ID, 0, resources.getString(R.string.help)).setIcon(R.drawable.ic_menu_help);
    menu.add(0, SETTINGS_ID, 0, resources.getString(R.string.settings)).setIcon(
        R.drawable.ic_menu_preferences);

    return clearQueue;
  }

  /**
   * Creates the options for the landscape menu. Always called before the
   * options menu is shown.
   */
  public void createOptionsMenuLandscape(Menu menu, Resources resources) {
    menu.add(0, ENQUEUE_FULL_LIST_ID, 0, resources.getString(R.string.enqueue_all)).setIcon(
        R.drawable.ic_menu_add);
  }

  /**
   * Calls the appropriate method on the {@link OptionsMenuHandler}. Always
   * called when an options menu item got clicked.
   */
  public boolean optionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case CLEAR_QUEUE_ID:
      optionsHandler.clearQueue();
      return true;
    case ENQUEUE_FULL_LIST_ID:
      optionsHandler.enqueueFullList();
      return true;
    case HELP_ID:
      optionsHandler.showDialog(DialogManager.DIALOG_HELP);
      return true;
    case SETTINGS_ID:
      optionsHandler.showSettings();
      return true;
    default:
      Logger.error("ERROR: This id is not an option: " + item.getItemId());
      return false;
    }
  }
}
